package top.humbleyuan.designmode.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author HumbleYuan
 * @Date 2019/12/18 19:09
 * @Des 教师扮演通知类
 */
public class Teacher implements Subject{
    // 存储观察者
    private List<Observer> observers;

    // 通知信息
    private String msg;

    public Teacher() {
        observers = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        if(!observers.isEmpty() && observers.contains(observer)) {
            observers.remove(observer);
        }
    }

    @Override
    public void notifyObserver() {
        for (Observer o : observers) {
            o.update(msg);
        }
    }

    // 更新通知信息，并进行通知
    public void setMessage(String s) {
        this.msg = s;
        System.out.println("教师的通知更新为:" + s);
        notifyObserver();
    }
}
